/*
 * LDIR
 *
 * LDIR, a GPS cartography application built for Let's Do It Romania eco
 * campaign.
 * Copyright (C) 2010 Genera Software <office@generasoftware.com>
 *
 * http://generasoftware.com/
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 */

// Purpose: Define macros to easily use the logger
// Prerequisites:
//      - define the KLogFile _LIT constant in the form _LIT(KLogFile, "c:\\logs\\appname\\log.txt");

#ifndef __LOGGER_H
#define __LOGGER_H

// SYSTEM INCLUDES
#include <e32std.h>
#include <eikenv.h>
#include <bautils.h>

// USER INCLUDES
#include "StringUtils.h"

enum TVerbosity
    {
    EVerbosityCore = 0x0001, // core: engine, server, sessions
    EVerbosityWS = 0x0002, // Web services
    EVerbosityHttp = 0x004,
    EVerbosityImageUtils = 0x0100, // Image utils
    EVerbosityAll = 0xffff,
    };

// The log full file name
_LIT(KLogFile, "c:\\logs\\ldir\\log.txt");

/// LOG macro definition
/// Does nothing if the __LOG__ macro is not defined.
/// Logs the given arguments in the same format as TDes::Format.
#ifdef __LOG__
#define LOG(lvl, args...) Logger::InstanceL()->Log(lvl, args)
#define LOGBUFFER(buffer) Logger::InstanceL()->LogTextL(buffer)
#define LOGSETTINGS(verbosity, filename) Logger::InstanceL()->SetSettingsL(verbosity, filename)
#define LOGDESTROY() delete Logger::InstanceL()

/// Logger class definition
/// Responsible for logging stuff to file.
/// It does not exist if the __LOG__ macro is not defined.
/// Therefore, it should not be used directly, only through LOG macro.
class Logger : public CBase, public TDes8Overflow, public TDes16Overflow
    {
public:
    ~Logger();

    static Logger* InstanceL();
    ///
    /// Logs a text into the log file.
    ///
    void LogTextL(const TDesC& aText);
    ///
    /// Logs a 8 bit text.
    ///
    void LogTextL(const TDesC8& aText);
    ///
    /// Formats the arguments and logs the formatted descriptor to file.
    ///
    void Log(TInt aVerbosity, TRefByValue<const TDesC> adFormat, ...);
    ///
    /// Formats the arguments and logs the formatted descriptor to file, 8 bit version.
    ///
    void Log(TInt aVerbosity, TRefByValue<const TDesC8> aFormat, ...);

    void SetSettingsL(TUint aVerbosity, const TDesC& aFilename);
private:

    void Overflow(TDes8 &aDes);
    void Overflow(TDes16 &aDes);

    Logger();
    TUint iVerbosity;
    RFs iFs;
    RFile iLogFile;
    RBuf8 iBuffer;
    static Logger* iLogger;
    };

#else

#define LOG(lvl, args...)
#define LOGBUFFER(buffer)
#define LOGSETTINGS(verbosity, filename)
#define LOGDESTROY()

#endif // __LOG__
#endif //__LOGGER_H
